package com.imddy.tweb.controller;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/fileupload/")
public class FileUploadController {

	private static Log log = LogFactory.getLog(FileUploadController.class);
	
	@RequestMapping("upload1")
	public String upload1(HttpServletRequest request, @RequestParam("file1") MultipartFile file1) {
		// 判断文件是否为空  
        if (!file1.isEmpty()) {
            try {
                // 文件保存路径 
                String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"
                        + file1.getOriginalFilename();
                // 转存文件  
                file1.transferTo(new File(filePath));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("UserName:" + username);
        System.out.println("Password:" + password);
        log.info("UserName:" + username);
        log.info("Password:" + password);
        // 重定向 
        return "redirect:/fileupload/filelist.do";
	}
	
	@RequestMapping("filelist")
	public ModelAndView filelist(HttpServletRequest request) {
		String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/";
        ModelAndView mav = new ModelAndView("/filelist");
        File uploadDest = new File(filePath);
        String[] fileNames = uploadDest.list();
        for (int i = 0; i < fileNames.length; i++) {
            //打印出文件名 
            System.out.println(fileNames[i]);
            System.out.println(filePath);
            log.info(fileNames[i]);
            log.info(filePath);
        }
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
        mav.addObject("uploadPath", basePath+"upload/");
        mav.addObject("fileNames", fileNames);
        return mav;
	}
	
	
	 @SuppressWarnings("unused")
	 private boolean saveFile(MultipartFile file) {  
	        // 判断文件是否为空  
	        if (!file.isEmpty()) {
	            try {
	                // 文件保存路径  
	                // String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/" + file.getOriginalFilename();
	            	String filePath = "/usr/local/upload/" + file.getOriginalFilename();
	                // 转存文件 
	                file.transferTo(new File(filePath));
	                return true;
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	        }
	        return false;
	 }
	 
	 private boolean saveFile2(HttpServletRequest request,MultipartFile file) {  
	        // 判断文件是否为空  
	        if (!file.isEmpty()) {
	            try {
	                // 文件保存路径  
	                String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/" + file.getOriginalFilename();
	                // 转存文件 
	                file.transferTo(new File(filePath));
	                return true;
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	        }
	        return false;
	 }
	 
	 @RequestMapping("upload2")  
	    public String upload2(@RequestParam("files") MultipartFile[] files, HttpServletRequest request) {  
	        //判断file数组不能为空并且长度大于0
	        if(files!=null&&files.length>0){
	            //循环获取file数组中得文件 
	            for(int i = 0;i<files.length;i++){
	                MultipartFile file = files[i];
	                //保存文件 
	                System.out.println("True&False : " + saveFile2(request,file));
	            }
	        }
	        String username = request.getParameter("username");
	        String password = request.getParameter("password");
	        System.out.println("2UserName:" + username);
	        System.out.println("2Password:" + password);
	        log.info("2UserName:" + username);
	        log.info("2Password:" + password);
	        // 重定向  
	        return "redirect:/fileupload/filelist.do";
	 }
	
}
